package com.splatter;


import java.util.ArrayList;
import java.util.List;

import javax.persistence.EntityManager;

import com.chat.chatter.ejb.Emf;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;


public class UsersServiceImpl implements UsersService {

	public String getUsers() {
		//my try
		EntityManager em = Emf.get().createEntityManager();
		
		/*User user = new User();
		user.setName(input);
		user.setUsername(input);
		user.setPassword("password");		
		String doesItExist = "does not exist.";*/
		List existing = new ArrayList();
		
		try {
	        // ... do stuff with em ...
			if ( null == em ) {
                System.out.println( "entityManagerFactory is hosed" );
			}
			
			//System.out.println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" + user.getUsername());
		    existing = em.createQuery(
		         "select u.username from User u")
		         .getResultList();

		    if ( existing.size() == 0 ) {
		    	System.out.println("!!!NO USERS EXIST!!!");
		        //entityManager.persist(user);
		        //em.persist(user);
		        //log.info("Registered new user #{user.username}");
		        //return "/registered.xhtml";
		        //System.out.println("created");
		    }
		    else {
		    	System.out.println("Users");
		    	//for (User u : existing ) {
		    		System.out.println(existing);
		    	//}
		    	System.out.println("!!!USERS EXIST!!!");
		    	//doesItExist = "exists";
		        //return null;
		    }
	    } finally {
	        em.close();
	    }
	    
		return "<br><br><br><br>User, " + existing;
	}
}
